package com.csdn.leon.view.dialog;

import com.csdn.leon.activity.BlogHostActivity;
import com.csdn.leon.adapter.DialogAdapter;
import com.csdn.leon.adapter.DialogAdapter.ClickedListener;
import com.csdn.leon.view.ClearEditText;
import com.csdn.leon.view.gridview.MyGridView;
import com.csdn.leon.base.R;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SearchDialog extends Dialog implements
		android.view.View.OnClickListener, ClickedListener {

	int layoutRes;// 布局文件
	Context context;
	private ClearEditText mClearEditText;
	private ImageView search;
	private MyGridView gridView;
	private DialogListener listener;
	private DialogAdapter adapter;
	private String[] arrys;

	public SearchDialog(Context context) {
		super(context);
	}

	/**
	 * 自定义布局的构造方法
	 * 
	 * @param context
	 * @param resLayout
	 */
	public SearchDialog(Context context, int resLayout) {
		super(context);
		this.context = context;
		this.layoutRes = resLayout;
	}

	/**
	 * 自定义主题及布局的构造方法
	 * 
	 * @param context
	 * @param theme
	 * @param resLayout
	 */
	public SearchDialog(Context context, int theme, int resLayout) {
		super(context, theme);
		this.context = context;
		this.layoutRes = resLayout;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layoutRes);
		setCancelable(true);
		initView();

	}

	private void initView() {
		arrys = context.getResources().getStringArray(R.array.username);
		mClearEditText = (ClearEditText) findViewById(R.id.selecedbank_edit);
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		search = (ImageView) findViewById(R.id.search);
		search.setOnClickListener(this);
		gridView = (MyGridView) findViewById(R.id.gridview);
		adapter = new DialogAdapter(context, arrys);
		adapter.setListener(this);
		gridView.setAdapter(adapter);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
	}

	public interface DialogListener {
		public void isCancel(boolean flag);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search:
			String editText = mClearEditText.getText().toString().trim();
			if (!TextUtils.isEmpty(editText)) {
				this.cancel();
				Intent intent = new Intent(context, BlogHostActivity.class);
				intent.putExtra("username", mClearEditText.getText().toString()
						.trim());
				context.startActivity(intent);
			}else{
				Toast.makeText(context, "请输入正确的博主信息", 2000).show();
			}
			break;
		}
	}

	public void setDialogListener(DialogListener listener) {
		this.listener = listener;
	}

	@Override
	public void cancleDialog() {
		this.cancel();

	}

}
